package com.ciban.smarthome.module.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import com.ciban.smarthome.R;
import com.ciban.smarthome.common.utils.FileUtils;
import com.ciban.smarthome.common.utils.ImgUtils;
import com.ciban.smarthome.common.utils.StringUtils;
import com.ciban.smarthome.module.entity.Area;
import com.lidroid.xutils.BitmapUtils;

import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class AreaGridAdapter extends BaseAdapter {
	private Context context;
	private List<Area> list;

	public AreaGridAdapter(Context context, ArrayList<Area> list) {
		this.context = context;
		this.list = list;
	}

	private boolean isModify = false;

	public void setModify(boolean isModify) {
		this.isModify = isModify;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = View.inflate(context,
					R.layout.griditem_common_content, null);
			holder = new ViewHolder();
			holder.iv_icon = (ImageView) convertView
					.findViewById(R.id.item_gv_content_iv_icon);
			holder.tv_title = (TextView) convertView
					.findViewById(R.id.item_gv_content_tv_title);
			holder.cb_sel = (CheckBox) convertView
					.findViewById(R.id.item_gv_content_sel);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Area info = list.get(position);

		// int path = ModuleUtils.getAreaIcon(info.getType());

		String path = info.getIcon_path();
		if (!StringUtils.isEmpty(path)) {

			String real_path = ImgUtils.getRealFilePath(context,
					Uri.parse(path));
			if (FileUtils.isFileExists(real_path)) {
				BitmapUtils utils = new BitmapUtils(context);
				utils.display(holder.iv_icon, real_path);
				utils = null;
			}
		}
		
		holder.tv_title.setText(info.getName());

		if (isModify) {
			holder.cb_sel.setVisibility(View.VISIBLE);
			if (info.isSel()) {
				holder.cb_sel.setChecked(true);
			} else {
				holder.cb_sel.setChecked(false);
			}
		} else {
			holder.cb_sel.setVisibility(View.GONE);
		}

		return convertView;
	}

	static class ViewHolder {
		ImageView iv_icon;
		TextView tv_title;
		CheckBox cb_sel;
	}

}
